package com.uenpay.im.ui.old.activity.videochat;

import android.content.Intent;
import android.text.TextUtils;

import com.qyx.android.database.DBTalkMsgColumns;
import com.qyx.android.database.TalkMsgManager;
import com.qyx.android.message.type.MsgContentType;
import com.qyx.android.message.type.MsgType;
import com.qyx.android.protocol.QyxMsg;
import com.uenpay.im.IMApplication;
import com.qyx.videochat.util.VideoChatResultType;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * @author SL
 * @created 2016-2-18 下午7:46:13
 * @copyRight http://www.qiyunxin.com
 * @function 处理视频聊天的离线消息
 */
public class VideoChatOffLineMsgManager {

	private TalkMsgManager mTalkMsgManager = new TalkMsgManager();
	private ArrayList<QyxMsg> offLineVideoChatMsgList = null;

	public VideoChatOffLineMsgManager() {
		offLineVideoChatMsgList = new ArrayList<QyxMsg>();
	}

	/**
	 * 添加视频聊天离线消息
	 *
	 * @param mQyxMsg
	 */
	public void addOffLineVideoChatMsg(QyxMsg mQyxMsg) {
		if (offLineVideoChatMsgList != null) {
			offLineVideoChatMsgList.add(mQyxMsg);
		}
	}

	/**
	 * 删除对应的视频聊天离线消息
	 *
	 * @param msg_no
	 */
	public void removeOffLineVideoChatMsg(String msg_no) {
		if (TextUtils.isEmpty(msg_no) || offLineVideoChatMsgList == null)
			return;
		for (int i = 0, size = offLineVideoChatMsgList.size(); i < size; i++) {
			if (offLineVideoChatMsgList.get(i).msg_no.equals(msg_no)) {
				offLineVideoChatMsgList.remove(i);
				break;
			}
		}
	}

	/**
	 * 获取对方还未挂断的视频聊天消息
	 */
	public void checkOnLineVideoChatMsg() {
		if (offLineVideoChatMsgList != null
				&& offLineVideoChatMsgList.size() > 0) {
			/** 存在未挂断的视频聊天消息 **/
			Intent intent = new Intent(IMApplication.getAppContext(),
					VideoChatResponseActivity.class);
			intent.putExtra("message", offLineVideoChatMsgList.get(0));
			intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			IMApplication.getAppContext().startActivity(intent);

			// TODO
			/** 将其他正在请求视频通话的消息回复为正在忙碌 **/
			offLineVideoChatMsgList.remove(0);
			if (offLineVideoChatMsgList.size() > 0) {
				for (int i = 0, size = offLineVideoChatMsgList.size(); i < size; i++) {
					replyVideoChatMsg(offLineVideoChatMsgList.get(i));
				}
			}
		}
	}

	/**
	 * 回复其他正在请求视频通话用户：忙碌
	 *
	 * @param mQyxMsg
	 */
	public void replyVideoChatMsg(QyxMsg mQyxMsg) {


		String content_json = mQyxMsg.content_json;
		if(!TextUtils.isEmpty(content_json)){
			try {
				JSONObject jsonObject = new JSONObject(content_json);
				if(jsonObject.has("video_chat_result")){
					jsonObject.remove("video_chat_result");
				}
				jsonObject.put("video_chat_result", VideoChatResultType.VideoChatResultTypeCancelCall);

				/** 将未读的视频通话消息类型改为对方已取消呼叫 **/
				mTalkMsgManager.updateFieldMsgByMsgNo(
						DBTalkMsgColumns.CONTENT_JSON,
						VideoChatResultType.VideoChatResultTypeCancelCall + "",
						mQyxMsg.msg_no);

			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

//		/** 将未读的视频通话消息类型改为对方已取消呼叫 **/
//		mTalkMsgManager.updateFieldMsgByMsgNo(
//				DBTalkMsgColumns.VIDEO_CHAT_RESULT,
//				VideoChatResultType.VideoChatResultTypeCancelCall+"",
//				mQyxMsg.msg_no);
		QyxMsg msg = new QyxMsg();
		msg.msg_content_type = MsgContentType.BUSY_VIDEO_CHAT;
		msg.from_cust_id = mQyxMsg.to_cust_id;
		msg.from_cust_name = IMApplication.config.getUserName();
		msg.to_cust_id = mQyxMsg.from_cust_id;
		//msg.chat_type = PacketType.PERSONAL;
		msg.msg_type = MsgType.MSG_SENDS + "";
		msg.content = mQyxMsg.msg_no;
		//msg.chat_id = 0;
		msg.sessionModel.setSessionId(0);
		msg.msg_no = msg.getMsgNo(msg.from_cust_id + "", msg.to_cust_id + "",
				msg.sessionModel.getSessionId() + "");
		//TODO IMApplication.getInstance().sendMessage(msg,null);

	}

}
